pub mod state;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
pub use state::AppState;

pub mod request;
pub use request::*;

pub mod response;
pub use response::*;
/**
 * 会话上下文
 */
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionContext {
    pub current_user: Option<CurrentUser>,
}

/// 当前用户信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CurrentUser {
    pub userid: String,
    pub username: String,
    pub roles: Vec<String>,
    pub status: String,
}

impl Into<Value> for CurrentUser {
    fn into(self) -> Value {
        serde_json::to_value(self).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{json, Value};

    use crate::{error::BizError, model::BizResponse};

    use super::{BizRequest, RequestForm};

    #[test]
    fn test1() {
        let err = BizError::DeserializeError;
        println!("{:?}", err.to_string());
        println!("{}", format!("{:?}", err));

        let resp = BizResponse::<String>::fail_with_biz_error(err, Some("EO"));

        println!("{:?}", resp);
    }

    #[test]
    fn test_from_form() {
        let form = RequestForm {
            service: "hello".to_string(),
            biz_content: Some(
                r#"{
                    "pageSize": 1,
                    "pageNum": 2
                }"#
                .to_string(),
            ),
        };

        let json = r#"{
            "pageSize": 1,
            "pageNum": 2
        }"#;

        let json_value = serde_json::from_str::<Value>(json);

        let res = BizRequest::from_form(form, None, None);

        println!("form={:?}", res);
    }
}
